<?php
/**
 * @deprecated
 * @see \App\Http\Controllers\Wechat\RecommendProductV2Controller
 *
 * 推荐商品的管理
 * Author: lyun
 * Date: 2018/4/12
 * Created by Panxsoft.
 */

namespace App\Http\Controllers\Wechat;

use App\Http\Controllers\ApiController;
use App\Http\Requests\Wechat\RecommendProductIndexRequest;
use App\Models\Panx\Product;
use App\Models\Panx\ProductTag;
use App\Models\Store\SkinRecord;
use App\Models\Store\Store;
use App\Services\Repositories\Product\DeprecatedRecommendProductRepository;
use App\Services\Utils\PanxCollection;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;

/**
 * @deprecated
 *
 * Class RecommendProductController
 *
 * @package App\Http\Controllers\Wechat
 */
class RecommendProductController extends ApiController
{
    /**
     * 根据肤质检测报告，推荐商品
     *
     * @param RecommendProductIndexRequest $request
     *
     * @return \Illuminate\Http\Response
     *
     * @throws \Exception
     */
    public function index(RecommendProductIndexRequest $request)
    {
        $skin_record = (new SkinRecord)->find($request->skin_record_id);
            $this->checkItemExist($skin_record, trans('wechat.skin_record'));
            $this->checkIsTrue($skin_record->skin_detector_id === (int)$request->skin_detector_id, trans('wechat.skin_detector'));
            $this->checkIsTrue($skin_record->wechatUser->openid === $request->openid, trans('wechat.wechat_user'));

        $tag_ids = (new DeprecatedRecommendProductRepository)->getTagIds($request);

        $recommend_products = $this->getProductsByTags($tag_ids, $request->sex, $skin_record);

        $recommend_products = $this->assembleProducts($recommend_products);

        return $this->response(compact('recommend_products'));
    }

    /**
     * @param array $tag_ids
     * @param $sex
     * @param SkinRecord $skin_record
     * @return Collection|static[]
     */
    private function getProductsByTags(array $tag_ids, $sex, SkinRecord $skin_record)
    {
        /** @var Store $store */
        $store = $skin_record->store;

        $recommend_products = (new Product)
            ->filterSex($sex)
            ->whereIn('store_id', [$store->id, Product::NO_STORE_ID])
            ->where('customer_id', $store->customer->id)
            ->whereHas('productTags', function (Builder $query) use ($tag_ids) {
                $query->whereIn(((new ProductTag)->getTable()) . '.id', $tag_ids);
            })
            ->with('thumbImage', 'productTags')
            ->inRandomOrder()
            ->limit(rand(5, 10))
            ->get();

        return $recommend_products;
    }

    /**
     * @param Collection $recommend_products
     * @return Collection
     */
    private function assembleProducts(Collection $recommend_products)
    {
        (new PanxCollection($recommend_products))
            ->eachAppend(['tag_names', 'thumb_url','link_product'])
            ->makeHidden(['thumbImage', 'productTags','productUrlType']);

        return $recommend_products;
    }
}
